package com.hnevc.netfoodchoose;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2017/11/24.
 */

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    List<Food> mFoodList;
    Context mContext;//传递过来的上下文对象

    public MyAdapter(List<Food> mFoodList, Context mContext) {
        this.mFoodList = mFoodList;
        this.mContext = mContext;
    }

    //创建要显示的ViewHolder对象
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //根据layout文件，创建视图
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.food_list_item, null);
        //根据视图获取viewHolder
        MyViewHolder viewHolder = new MyViewHolder(itemView);

        return viewHolder;
    }

    //根据每一项数据，确定每一个列表项显示的外观
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        //将ViewHolder，转换为MyViewHolder
        MyViewHolder myViewHolder = (MyViewHolder) holder;
        //获取datas【position】的值，作为TextView的文本显示
        Food food = mFoodList.get(position);
        myViewHolder.tvFoodName.setText( food.getFoodName());
        String imageurl = "http://10.1.66.232:8080/RestaurantServer/uploadFoodImage/"
                +food.getFoodPic();
        Picasso.with(mContext).load(imageurl).into(myViewHolder.imageView);
        myViewHolder.tvFoodDesc.setText(food.getFoodDesc());
        myViewHolder.tvFoodPrice.setText(food.getFoodPrice()+"");

    }
    //返回RecyclerView控件需要显示的数据项的数量
    @Override
    public int getItemCount() {
        return mFoodList.size();
    }

    //创建一个ViewHolder的内部类
    class MyViewHolder extends  RecyclerView.ViewHolder{
        ImageView imageView;//显示图片的控件
        TextView tvFoodName;//显示文本的控件
        TextView tvFoodPrice;
        TextView tvFoodDesc;

        public MyViewHolder(View itemView/*item视图*/) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.id_iv_foodPic);
            tvFoodName = (TextView) itemView.findViewById(R.id.id_tv_foodName);
            tvFoodPrice = (TextView) itemView.findViewById(R.id.id_tv_foodPrice);
            tvFoodDesc = (TextView) itemView.findViewById(R.id.id_tv_foodDesc);
        }
    }
}
